<template>
  <div class="timeline-page">
    <h1 class="timeline-title">文章时间轴</h1>
    <div class="timeline-container">
      <div class="timeline">
        <div 
          class="timeline-item" 
          v-for="article in articles" 
          :key="article.id"
          @click="viewArticle(article.id)"
        >
          <div class="timeline-dot"></div>
          <div class="timeline-date">{{ formatDate(article.createTime) }}</div>
          <div class="timeline-content">
            <h3>{{ article.title }}</h3>
            <p>{{ article.summary }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getArticleList } from '@/api/article';

const router = useRouter();
const articles = ref([]);
const loading = ref(false);

onMounted(async () => {
  await fetchArticles();
});

const fetchArticles = async () => {
  loading.value = true;
  try {
    const response = await getArticleList({
      pageNum: 1,
      pageSize: 20 // 增加数量以显示更多文章
    });
    
    if (response && response.records) {
      articles.value = response.records;
    }
  } catch (error) {
    console.error('获取文章列表失败:', error);
  } finally {
    loading.value = false;
  }
};

const formatDate = (dateString) => {
  if (!dateString) return '';
  
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  
  return `${year}-${month}-${day}`;
};

const viewArticle = (id) => {
  router.push(`/article/${id}`);
};
</script>

<style scoped>
.timeline-page {
  padding: 20px;
  max-width: 900px; /* 更宽的容器 */
  margin: 0 auto; /* 居中显示 */
}

.timeline-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 2rem;
  font-weight: 700;
  position: relative;
  padding-bottom: 15px;
}

.timeline-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #42b983, #64b5f6);
  border-radius: 3px;
}

.timeline-container {
  position: relative;
  padding: 20px 0;
}

.timeline {
  position: relative;
}

.timeline::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 120px;
  width: 3px;
  background: linear-gradient(to bottom, #42b983, #64b5f6);
  z-index: 1;
}

.timeline-item {
  position: relative;
  padding-left: 150px;
  margin-bottom: 40px;
  cursor: pointer;
  transition: transform 0.3s;
}

.timeline-item:hover {
  transform: translateX(10px);
}

.timeline-item:last-child {
  margin-bottom: 0;
}

.timeline-dot {
  position: absolute;
  left: 116px;
  top: 5px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background-color: #fff;
  border: 3px solid #42b983;
  z-index: 2;
}

.timeline-date {
  position: absolute;
  top: 0;
  left: 0;
  width: 100px;
  text-align: right;
  font-weight: 600;
  color: #42b983;
}

.timeline-content {
  padding: 15px 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.1);
  transition: box-shadow 0.3s;
}

.timeline-content:hover {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
}

.timeline-content h3 {
  margin: 0 0 10px;
  font-size: 1.2rem;
  color: #333;
}

.timeline-content p {
  margin: 0;
  color: #666;
  font-size: 0.95rem;
  line-height: 1.5;
  max-height: 4.5em;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

@media (max-width: 768px) {
  .timeline::before {
    left: 30px;
  }
  
  .timeline-item {
    padding-left: 60px;
  }
  
  .timeline-dot {
    left: 26px;
  }
  
  .timeline-date {
    position: relative;
    left: auto;
    top: auto;
    width: auto;
    text-align: left;
    margin-bottom: 10px;
  }
}
</style> 